<?php
namespace app\admin\service;
use Curl\Curl;
use think\Exception;

//腾讯位置服务： https://lbs.qq.com/webApi/uriV1/uriGuide/uriWebPoisearch
//腾讯位置服务： https://lbs.qq.com/service/webService/webServiceGuide/webServiceSuggestion
class TencentService
{

    protected $apiUrl = null;
    protected $apiKey = null;
    protected $apiSign = null;
    protected $errJson = '{"result":"error","code":500,"msg":"接口调用失败！"}';

    public function __construct(Curl $curl)
    {
        //签名计算方法： https://lbs.qq.com/FAQ/server_faq.html#3
        $this->apiUrl = 'https://apis.map.qq.com';
        $this->apiKey = trim(\Config::get('qq_lbs.apiKey'));
        $this->apiSign = trim(\Config::get('qq_lbs.apiSign'));

        $this->curl = $curl;
    }

    //地点搜索接口：参考：https://lbs.qq.com/service/webService/webServiceGuide/webServiceSuggestion
    public function searchAddr($keyword, $region=''){
        $rows = [];
        try{
            $params = array(
                'keyword' => $keyword,
                'region' => $region,
                'key' => $this->apiKey,
            );
            ksort($params);     //文档要求：先升序排列

            $string = '/ws/place/v1/suggestion/?' . urldecode(http_build_query($params));   //文档要求：非编码
            $md5Sign = md5(  $string. $this->apiSign );

            $params['sig'] = $md5Sign;
            $retApi = $this->curl->get($this->apiUrl .$string .'&sig='.$params['sig']);
            if($retApi->status === 0){
                return $retApi->data;
            }

        }catch (Exception $e){
            //log('呼叫中心接口错误：获取分机初始状态');
        }
        return $rows;
    }


}